package com.hhh.demo.sx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.demo.sx.bean.Record;
import com.hhh.demo.sx.bean.Shop;
import com.hhh.demo.sx.bean.Store;
import com.hhh.demo.sx.bean.User;
import com.hhh.demo.sx.mapper.RecordMapper;
import com.hhh.demo.sx.service.RecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhh.demo.sx.util.ResultPage;
import com.hhh.demo.sx.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhh
 * @since 2022-01-04
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    @Autowired
    private RecordMapper recordMapper;
    @Override
    public void addMemory(Record record) {
        recordMapper.insert(record);
    }



    @Override
    public Map<String, Object> findAll(int p, int limit, String condition) {
        IPage<Record> page = null;
        QueryWrapper<Record> warpper = new QueryWrapper<>();
        Page<Record> pages = new Page<>(p, limit);

        JSONObject jsonObject = JSONObject.parseObject(condition);
        if (jsonObject != null) {
            String username = (String) jsonObject.get("username");
            warpper.like(!username.equals(""), "username", username);
            page= recordMapper.selectPage(pages,warpper);
        } else {
            page= recordMapper.selectPage(pages,null);
        }
        return ResultPage.Page(page.getRecords(), page.getTotal());
    }

}
